﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;

namespace FourWindsBar.WebServicesAccess
{
    public class RESTServices
    {
        /// <summary>
        /// Generic REST call procedure. Yes I know we can now do this with WCF but System.Net is easier and cleaner to my mind.
        /// </summary>
        /// <param name="restCALL">The REST call to be made</param>
        /// <param name="result">Result of successful REST call</param>
        /// <returns>Boolean indicating success</returns>
        public static void MakeRESTcall(string restCALL, out string result)
        {
            result = string.Empty;
            
            try
            {
                HttpWebRequest request = WebRequest.Create(restCALL) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    result = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                //Just pass back result it's up to the caller to throw the exception.
                result = ("Error making RESTCall: " + restCALL + "\r\r" + ex.Message);
            } // end try

        } // end makeRESTcall

    }
}
